package com.hongxun.design.fragments;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.ProjectDetailsActivity;
import com.hongxun.design.activitys.ProjectMapActivity;
import com.hongxun.design.adapters.MaterialAdapter;
import com.hongxun.design.adapters.PoleSunAdapter;
import com.hongxun.design.adapters.ProjectDetailsAdapter;
import com.hongxun.design.adapters.SectionAdapter;
import com.hongxun.design.adapters.UserPointAdapter;
import com.hongxun.design.bean.CreateProjectBean;
import com.hongxun.design.bean.ExplorSumBean;
import com.hongxun.design.bean.ExplorSumListBean;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.bean.PointSumBean;
import com.hongxun.design.bean.PointSumRouteBean;
import com.hongxun.design.bean.PoleSumBean;
import com.hongxun.design.bean.SectionBean;
import com.hongxun.design.bean.TransFormerMaterialBean;
import com.hongxun.design.bean.UserPointBean;
import com.hongxun.design.bean.photopassbean.SystemTypeBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.object_properties.MyMessage;
import com.hongxun.design.ui.titlepopupwindow.BitTypeBean;
import com.hongxun.design.ui.ui_listview.refreshlist.SwipeRefreshListView;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.DensityUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.fileutil.GetFileUtil;
import com.hongxun.design.utils.fileutil.SetFileUtil;
import com.hongxun.design.utils.httputil.HttpStringCodeTableUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;
import com.qx.wz.util.internal.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc on 2016/6/5.
 */
public class ProjectDetailsFragment extends BaseFragment {
    /**
     * 通用*
     */
    int type;
    SwipeRefreshListView refreshListView;
    String explor_id;//台区id
    String voltageGrade;//电压等级
    /**
     * 项目基础信息*
     */
    private Handler baseHandler;
    List<CreateProjectBean> base_data;
    ProjectDetailsAdapter details_base_Adapter;
    /**
     * 塔杆明细*
     */
    TextView details_words;
    private Handler pointSumHandler;
    private Handler poleSumHandler;
    LinearLayout details_voltagegrade;
    List<TextView> views = new ArrayList<>();
    List<PoleSumBean> poleSumBeans;
    PoleSunAdapter poleSunAdapter;
    PointSumBean bean;
    /**
     * 交叉跨越*
     */
    TextView crossing_words;
    LinearLayout crossing_transformer;
    private Handler transformerListHandler;
    private Handler sectionCorssHandler;
    List<BitTypeBean> bitTypeBeans = new ArrayList<>();
    List<TextView> crossing_views = new ArrayList<>();
    SectionBean sectionBean;
    SectionAdapter sectionAdapter;
    /**
     * 材料统计*
     */
    LinearLayout materialstatistics_transformer;
    List<TextView> materialstatistics = new ArrayList<>();
    private Handler materialSumHandler;
    List<TransFormerMaterialBean> materialBeans;
    MaterialAdapter materialAdapter;
    /**
     * 用电户*
     */
    private Handler userPointHandler;
    List<UserPointBean> userPointBeans;
    UserPointAdapter userPointAdapter;

    /**
     * 问题汇总
     */
    private Handler handlerPro;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contactsLayout = inflater.inflate(R.layout.fragment_projectdetailstab,
                container, false);
        getBundle();
        findView(contactsLayout);
        return contactsLayout;
    }

    private String texttype = MString.code_zero, id = MString.code_null, pointType;

    @SuppressLint("NewApi")
    private void getBundle() {
        Bundle bundle = getArguments();
        type = bundle.getInt(MString.TYPE);
        pointType = bundle.getString(MString.exploration_type, "");
        texttype = bundle.getString(MString.TextTYPE, MString.code_zero);
        id = bundle.getString(MString.explor_id, MString.code_null);
        switch (type) {
            case Constant.basicinformation:
                AddBaseHandler();
                break;
            case Constant.columndetail:
                addPointSumHandler();
                addPoleSumHandler();
                break;
            case Constant.crossing:
                addTransformerListHandler();//台区
                addSectionCorssHandler();
                break;
            case Constant.materialstatistics:
                addTransformerListHandler();//台区
                addmaterialSumHandler();
                break;
            case Constant.electrichousehold:
                addUserPointHandler();
                break;
            case Constant.problems:

                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == Constant.basicinformation) getBaseData();
    }

    @Override
    void findView(View v) {
        super.findView(v);
        refreshListView = (SwipeRefreshListView) v.findViewById(R.id.listview_details);//数据控件
        switch (type) {
            case Constant.basicinformation:
                break;
            case Constant.columndetail:
                findView_columndetail(v);
                break;
            case Constant.crossing:
                findView_crossing(v);
                break;
            case Constant.materialstatistics:
                findView_materialstatistics(v);
                break;
            case Constant.electrichousehold:
                findView_UserPoint(v);
                break;
            case Constant.problems:
                break;
        }

        refreshListView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {//刷新函数
            @Override
            public void onRefresh() {
                switch (type) {
                    case Constant.basicinformation:
                        getBaseData();
                        break;
                    case Constant.columndetail:
                        getPoleSum();
                        break;
                    case Constant.crossing:
                        getSectionCorss();
                        break;
                    case Constant.materialstatistics:
                        getMaterialSum();
                        break;
                    case Constant.electrichousehold:
                        getUserPoint();
                        break;
                    case Constant.problems:
                        break;
                }
            }
        });
        initialization();
    }

    @Override
    void initialization() {
        super.initialization();
//        refreshListView.setAdapter();
        switch (type) {
            case Constant.basicinformation:
//                String BaseJson = MyApp.File.ReadFileData(MString.ProjectStageBaseInfo + "_" + MyApp.currProject.getStage_id());
//                if (BaseJson != null && BaseJson.length() > 0) {
//                    baseHandler.sendMessage(MyMessage.getMessage(HttpStringUtil.SUCCESSED, BaseJson));
//                }
                //MyApp.File.ReadFileToSend(baseHandler, MString.ProjectStageBaseInfo + "_" + MyApp.currProject.getStage_id());
                getBaseData();
                break;
            case Constant.columndetail:
//                if (ProjectMapActivity.s.length() > 0) {
//                    details_words.setText(ProjectMapActivity.s);
//                } else {
//                    String SumJson = MyApp.File.ReadFileData(MString.ProjectStageTotalInfo + "_" + MyApp.currProject.getStage_id());
//                    if (SumJson != null && SumJson.length() > 0) {
//                        bean = FastJsonUtil.JsonToGetPointSum(SumJson);
//                        pointSumHandler.sendEmptyMessage(1);
//                    }

                // MyApp.File.ReadFileToSend(pointSumHandler, MString.ProjectStageTotalInfo + "_" + MyApp.currProject.getStage_id());
                getpointSum();
//                }
                break;
            case Constant.crossing:
                refreshListView.setVisibility(View.VISIBLE);
                break;
            case Constant.materialstatistics:
                break;
            case Constant.electrichousehold:
                break;
            case Constant.problems:
                break;
        }
    }

    /***********************************项目基础信息*************************************************/
    /**
     * 获取项目信息*
     */
    private void getBaseData() {
        if (MyApp.isOnLine) {
            MyApp.HTTP.getProjectInfo(baseHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id());
        } else {
            baseHandler.sendMessage(MyMessage.getMessage(HttpStringUtil.SUCCESSED, GetFileUtil.getProjectInfo()));
        }
    }

    /**
     * 获取项目信息Handler*
     */
    private void AddBaseHandler() {
        baseHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        MyLog.L(MString.SUCCESSED + msg.obj);
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            SetFileUtil.setProjectInfo((String) msg.obj);
                            if (JSON.parseObject((String) msg.obj).size() > 0) {
                                com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject((String) msg.obj);
                                String o = JSON.parseObject((String) msg.obj).get("project_name").toString();
                                if (!texttype.equals(MString.code_one)) {
                                    ((ProjectDetailsActivity) getActivity()).bit_layout.setTitle(o);
                                }
                                MyApp.currProject.setProject_name(o);
                                if (base_data != null) {
                                    base_data.clear();
                                    base_data.addAll(DataUtil.getJGCreateItemData());
                                } else {
                                    base_data = DataUtil.getJGCreateItemData();
                                }

                                for (CreateProjectBean b : base_data) {
                                    if (jsonObject.containsKey(b.getSendCode())) {
                                        switch (b.getItemType()) {
                                            case Constant.CREATE_GENERAL:
                                                switch (b.getIsWrite()) {
                                                    case Constant.CLICKEDTEXT:
                                                        b.setValue(jsonObject.getString(b.getSendCode()));
                                                        break;
                                                    case Constant.CLICKPOP:
                                                        b.setValuecode(jsonObject.getString(b.getSendCode()));
                                                        break;
                                                    case Constant.CLICKACTIVITY:
                                                        if (b.getSendCode().equals(MString.codeset_version_id)) {
                                                            b.setValue(jsonObject.getString(MString.codeset_version));
                                                            b.setValuecode(jsonObject.getString(b.getSendCode()));
                                                        } else {
                                                            b.setValue(jsonObject.getString(b.getSendCode()));
                                                        }
                                                        break;

                                                }
                                                break;
                                            case Constant.CREATE_REMARKS:
                                                b.setValue(jsonObject.getString(b.getSendCode()));
                                                break;
                                        }
                                    }
                                }
                                baseHandler.sendEmptyMessage(1);
                            }
                        } else {
                            refreshListView.setRefreshing(false);
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (details_base_Adapter == null) {
                            details_base_Adapter = new ProjectDetailsAdapter(base_data, getActivity());
                            refreshListView.setAdapter(details_base_Adapter);
                        } else {
                            refreshListView.update();
                            refreshListView.setRefreshing(false);
                        }

                        break;
                    case 2:
                        break;
                    case HttpStringUtil.ERROR:
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;
                }
            }
        };
    }
    /***********************************塔杆明细****************************************************/
    /**
     * 获取项目详情信息*
     */
    private void getpointSum() {
        if ((texttype.equals(MString.code_one) && id.length() > 0 && !id.equals("-1"))) {
            MyApp.HTTP.getExplorSum(pointSumHandler, 2, id);
        } else if (!texttype.equals(MString.code_one)) {
            MyApp.HTTP.getPointSum(pointSumHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id(), "");
        } else if (texttype.equals(MString.code_one)) {
            MyApp.HTTP.getPointSum(pointSumHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id(), id);
        }
    }

    /**
     * 项目详情信息接收器*
     */
    private void addPointSumHandler() {
        pointSumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            bean = FastJsonUtil.JsonToGetPointSum((String) msg.obj);
                            pointSumHandler.sendEmptyMessage(1);
                        } else {
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 2: {

                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            ExplorSumBean bean = FastJsonUtil.JsonToGetExplorSum((String) msg.obj);
                            String s = "";
                            if (bean != null) {
                                List<SystemTypeBean> transCapacitylist = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.transCapacity);
                                for (SystemTypeBean t : transCapacitylist) {
                                    if (t.getTypecode().equals(bean.trans_capacity)) {
                                        s += "额定容量:" + t.getTypename();
                                        break;
                                    }
                                }

                                s += " 杆塔:" + bean.pole_num + "基";
                                if (bean.route != null && bean.route.size() > 0) {
                                    for (ExplorSumListBean b : bean.route) {
                                        String voltageGrade = DataUtil.getStringFormSystemTypeBean(HttpStringCodeTableUtil.voltageGrade, b.voltage_grade);
                                        if (voltageGrade.length() > 0) {
                                            try {

                                                if (b.line_length != null && b.line_length.length() > 0) {
                                                    s += "\n" + voltageGrade + "线路:";
                                                    s += b.line_length + "Km";
                                                }
                                                if (b.line_pole != null && b.line_pole.length() > 0) {
                                                    s += " 杆塔:" + b.line_pole + " 基";
                                                }

                                            } catch (Exception e) {
                                            }
                                        }


                                    }
                                }

                            }
                            details_words.setText(s);
                            details_words.setVisibility(View.VISIBLE);
                        } else {
                            details_words.setText("");
                            details_words.setVisibility(View.GONE);
                        }
                    }
                    break;
                    case 1:
                        String s = "";
                        if (bean != null) {
                            if (bean.trans_num != null) s += "变压器:" + bean.trans_num + "台";
                            if (bean.pole_num != null) s += " 杆:" + bean.pole_num + "基";
                            if (bean.user_num != null) s += " 用电户:" + bean.user_num + "户";

                            if (bean.route != null && bean.route.size() > 0) {
                                for (PointSumRouteBean b : bean.route) {
                                    String voltageGrade = DataUtil.getStringFormSystemTypeBean(HttpStringCodeTableUtil.voltageGrade, b.voltage_grade);
                                    if (voltageGrade.length() > 0) {
                                        try {
                                            if (b.line_length != null && b.line_length.length() > 0) {
                                                s += "\n" + voltageGrade + "线路:";
                                                s += b.line_length + "Km";
                                            }
                                            if (b.line_pole != null && b.line_pole.length() > 0) {
                                                s += " 杆塔:" + b.line_pole + " 基";
                                            }
                                        } catch (Exception e) {
                                        }
                                    }

                                }
                            }

                        }
                        details_words.setText(s);
                        if (StringUtils.isEmpty(s)) {
                            details_words.setVisibility(View.GONE);
                        } else {
                            details_words.setVisibility(View.VISIBLE);
                        }
                        break;
                    case HttpStringUtil.ERROR:
                        // if (bean != null) pointSumHandler.sendEmptyMessage(1);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;

                }
            }
        };
    }

    //获取杆塔明细信息
    private void getPoleSum() {
        if (poleSumBeans != null) {
            poleSumBeans.clear();
            poleSunAdapter.notifyDataSetChanged();
        }
        MyApp.HTTP.getPoleSum(poleSumHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id(), voltageGrade);
    }

    /**
     * 项目详情信息接收器*
     */
    private void addPoleSumHandler() {
        poleSumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (poleSumBeans == null) {
                                poleSumBeans = FastJsonUtil.JsonToGetPoleSumList((String) msg.obj);
                            } else {
                                poleSumBeans.clear();
                                poleSumBeans.addAll(FastJsonUtil.JsonToGetPoleSumList((String) msg.obj));
                            }
                            poleSumHandler.sendEmptyMessage(1);
                        } else {
                            refreshListView.setRefreshing(false);
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (poleSunAdapter == null) {
                            poleSunAdapter = new PoleSunAdapter(getActivity(), poleSumBeans);
                            refreshListView.setAdapter(poleSunAdapter);

                        } else {

                            refreshListView.update();
                            refreshListView.setRefreshing(false);
                        }
                        break;
                    case HttpStringUtil.ERROR:
//                        String ProjectStageTowerJson = MyApp.File.ReadFileData(MString.Project_+MyApp.currProject.getStage_id()+"/"+MString.ProjectStageTower + "_" + MyApp.currProject.getStage_id() + "_" + voltageGrade);
//
//                        if (ProjectStageTowerJson != null && ProjectStageTowerJson.length() > 0) {
//                            if (poleSumBeans == null) {
//                                poleSumBeans = FastJsonUtil.JsonToGetPoleSumList(ProjectStageTowerJson);
//                            } else {
//                                poleSumBeans.clear();
//                                poleSumBeans.addAll(FastJsonUtil.JsonToGetPoleSumList(ProjectStageTowerJson));
//                            }
//                        }
//                        poleSumHandler.sendEmptyMessage(1);
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        // MyApp.File.ReadFileToSend(poleSumHandler, MString.ProjectStageTower + "_" + MyApp.currProject.getStage_id() + "_" + voltageGrade);
                        break;

                }
            }
        };
    }

    /***********************************交叉跨越****************************************************/
    /**
     * 获取台区*
     */
    private void getTransformerList() {
        MyApp.HTTP.getTransformerList(transformerListHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id());
    }

    /**
     * 台区信息接收器*
     */
    private void addTransformerListHandler() {
        transformerListHandler = new Handler() {
            @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            List<Exploration> DATA = FastJsonUtil.JsonToGetExplorationList((String) msg.obj);
                            bitTypeBeans.clear();
                            if (texttype.equals(MString.code_zero)) {
                                bitTypeBeans.add(new BitTypeBean("", "全部", "全部"));
                            }
                            if (DATA != null && DATA.size() > 0) {
                                for (Exploration e : DATA) {
                                    bitTypeBeans.add(new BitTypeBean(e.exploration_id, "台区" + e.number));
                                }
                            }
                        } else {

                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        transformerListHandler.sendEmptyMessage(1);
                        break;
                    case 1://显示台区
                        MyLog.L(MString.SUCCESSED + "" + bitTypeBeans.size());
                        if (type == Constant.crossing) {
                            for (BitTypeBean b : bitTypeBeans) {
                                if (texttype.equals(MString.code_one) && id.length() > 0 && id.equals(b.getType())) {
                                    //台区
                                    addViewTransFormer(b);
                                } else if (texttype.equals(MString.code_zero)) {
                                    addViewTransFormer(b);
                                }

                            }
                            if (crossing_views.size() > 0) {
                                crossing_views.get(0).callOnClick();
                                MyLog.L(MString.SUCCESSED + "callOnClick");
                            } else {
                                refreshListView.setUpdate("无台区");
                            }
                            MyLog.L(MString.SUCCESSED + "sss");
                        } else {
                            for (BitTypeBean b : bitTypeBeans) {
                                if (texttype.equals(MString.code_one) && id.length() > 0 && id.equals(b.getType())) {
                                    addViewMaterialstatistics(b);
                                } else if (texttype.equals(MString.code_zero)) {
                                    addViewMaterialstatistics(b);
                                }
                            }
                            if (materialstatistics.size() > 0) {
                                materialstatistics.get(0).callOnClick();
                            } else {
                                refreshListView.setUpdate("无台区");
                            }
                        }

                        break;
                    case HttpStringUtil.ERROR:
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;

                }
            }
        };
    }

    /**
     * 阶段交叉跨越数据*
     */
    private void getSectionCorss() {
        // if (explor_id != null && explor_id.length() > 0) {
        if (sectionBean != null && sectionBean.list != null) {
            sectionBean.list.clear();
            sectionAdapter.notifyDataSetChanged();
        }
        MyApp.HTTP.getSectionCorss(sectionCorssHandler, HttpStringUtil.SUCCESSED, explor_id, pointType);
    }

    /**
     * 台区信息接收器*
     */
    private void addSectionCorssHandler() {
        sectionCorssHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (sectionBean == null) {
                                sectionBean = FastJsonUtil.JsonToGetSectionCorss((String) msg.obj);
                            } else {
                                SectionBean bean = FastJsonUtil.JsonToGetSectionCorss((String) msg.obj);
                                sectionBean.describe = bean.describe;
                                if (sectionBean.list != null) {
                                    sectionBean.list.clear();
                                    sectionBean.list.addAll(bean.list);
                                }
                            }

                            if (sectionBean.describe.length() == 0) {
                                crossing_words.setVisibility(View.GONE);
                            } else {
                                crossing_words.setText(sectionBean.describe);
                                crossing_words.setVisibility(View.VISIBLE);
                            }
                            sectionCorssHandler.sendEmptyMessage(1);
                        } else {
                            refreshListView.setRefreshing(false);
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        MyLog.L(MString.SUCCESSED + JSON.toJSONString(sectionBean.list));
                        if (sectionAdapter == null) {
                            sectionAdapter = new SectionAdapter(getActivity(), sectionBean.list);
                            refreshListView.setAdapter(sectionAdapter);
                            MyLog.L(MString.SUCCESSED + "加载");
                        } else {
                            refreshListView.update();
                            refreshListView.setRefreshing(false);
                            MyLog.L(MString.SUCCESSED + "刷新");
                        }

                        break;

                    case HttpStringUtil.ERROR:
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;

                }
            }
        };
    }

    /***********************************材料统计****************************************************/
    /**
     * 台区材料*
     */

    private void getMaterialSum() {
        if (materialBeans != null) {
            materialBeans.clear();
            materialAdapter.notifyDataSetChanged();
        }
        MyApp.HTTP.getMaterialSum(materialSumHandler, HttpStringUtil.SUCCESSED, explor_id, pointType);
    }

    /**
     * 加载台区材料handler*
     */
    private void addmaterialSumHandler() {
        materialSumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (materialBeans == null) {
                                materialBeans = FastJsonUtil.JsonToGetmaterialSum((String) msg.obj);
                            } else {
                                materialBeans.clear();
                                materialBeans.addAll(FastJsonUtil.JsonToGetmaterialSum((String) msg.obj));
                            }
                            materialSumHandler.sendEmptyMessage(1);
                        } else {
                            refreshListView.setRefreshing(false);
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (materialAdapter == null) {
                            materialAdapter = new MaterialAdapter(getActivity(), materialBeans);
                            refreshListView.setAdapter(materialAdapter);
                        } else {
                            refreshListView.update();
                            refreshListView.setRefreshing(false);
                        }
                        break;
                    case HttpStringUtil.ERROR:
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;
                }
            }
        };
    }
    /**
     * ********************************用电户*****************************************************
     */
    /**
     * 阶段用电户*
     */
    private void getUserPoint() {
        if (userPointBeans != null) {
            userPointBeans.clear();
            userPointAdapter.notifyDataSetChanged();
        }
        MyApp.HTTP.getUserPoint(userPointHandler, HttpStringUtil.SUCCESSED, MyApp.currProject.getStage_id());
    }

    /**
     * 阶段用电户信息接收*
     */
    private void addUserPointHandler() {
        userPointHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
                            if (userPointBeans == null) {
                                userPointBeans = FastJsonUtil.JsonToGetUserPoint((String) msg.obj);
                            } else {
                                userPointBeans.clear();
                                userPointBeans.addAll(FastJsonUtil.JsonToGetUserPoint((String) msg.obj));
                            }
                            userPointHandler.sendEmptyMessage(1);
                        } else {
                            refreshListView.setRefreshing(false);
                            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 1:
                        if (userPointAdapter == null) {
                            userPointAdapter = new UserPointAdapter(getActivity(), userPointBeans);
                            refreshListView.setAdapter(userPointAdapter);
                        } else {
                            refreshListView.update();
                            refreshListView.setRefreshing(false);
                        }

                        break;
                    case HttpStringUtil.ERROR:
                        refreshListView.setRefreshing(false);
                        ToastShow.showShort(getActivity(), (String) msg.obj);
                        break;
                }
            }
        };
    }

    /*************************************杆塔明细控件初始化**********************************************************/
    /**
     * columndetail控件*
     */
    private void findView_columndetail(View v) {
        RelativeLayout columndetail = (RelativeLayout) v.findViewById(R.id.columndetail_item);
        columndetail.setVisibility(View.VISIBLE);
        details_words = (TextView) columndetail.findViewById(R.id.details_words);
        details_voltagegrade = (LinearLayout) columndetail.findViewById(R.id.details_voltagegrade);
        setVoltageGrade();
    }

    /**
     * 动态加载电压等级*
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
    private void setVoltageGrade() {

        ArrayList<SystemTypeBean> voltageGradeData = new ArrayList<>();
        if (texttype.equals(MString.code_zero)) {
            voltageGradeData.add(new SystemTypeBean("", "全部"));
        }
        List<SystemTypeBean> v = FastJsonUtil.JsonToData_Code(HttpStringCodeTableUtil.voltageGrade);
        if (v != null && v.size() > 0) {
            voltageGradeData.addAll(v);
        }

        for (SystemTypeBean s : voltageGradeData) {
            if (!(s.getTypecode().equals(MString.highVoltage) && texttype.equals(MString.code_one)) && !id.equals("-1")) {
                addViewTomLinearLayout(s);
            } else if ((s.getTypecode().equals(MString.highVoltage) && texttype.equals(MString.code_one)) && id.equals("-1")) {
                addViewTomLinearLayout(s);
            }

        }
        if (views.size() > 0) {
            views.get(0).callOnClick();
        }

    }

    /**
     * 加载上部图标*
     */
    private void addViewTomLinearLayout(SystemTypeBean s) {
        MyLog.L(MString.SUCCESSED + " SystemTypeBean=" + JSON.toJSONString(s));
        RelativeLayout layout = new RelativeLayout(getActivity());
        TextView view = new TextView(getActivity());
        view.setText(" " + s.getTypename() + " ");
        view.setTextSize(16);
        view.setTextColor(getResources().getColor(R.color.gray));
        view.setGravity(Gravity.CENTER);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(getActivity(), 36));
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(view, params);
        views.add(view);
        view.setOnClickListener(new MClickListener(0, s) {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view, int position, Object msg) {
                if (views.size() > 0) {
                    for (TextView textView : views) {
                        ((TextView) view).setTextColor(getResources().getColor(R.color.gray));
                        textView.setBackground(null);
                    }
                }
                view.setBackgroundResource(R.drawable.circular6_blue_line3);
                ((TextView) view).setTextColor(getResources().getColor(R.color.caikan_bule));
                voltageGrade = ((SystemTypeBean) msg).getTypecode();
                getPoleSum();
            }
        });
        final float scale = getActivity().getResources().getDisplayMetrics().density;
        int left = (int) (28 * scale + 0.5f);
        layout.setPadding(left, 0, left, 0);
        details_voltagegrade.addView(layout, RelativeLayout.LayoutParams.WRAP_CONTENT, details_voltagegrade.getHeight() - 4);
    }
    /*************************************交叉跨越控件初始化**********************************************************/

    /**
     * crossing*
     */
    private void findView_crossing(View v) {
        RelativeLayout crossing = (RelativeLayout) v.findViewById(R.id.crossing_item);
        crossing.setVisibility(View.VISIBLE);
        crossing_words = (TextView) crossing.findViewById(R.id.crossing_words);
        crossing_transformer = (LinearLayout) crossing.findViewById(R.id.crossing_transformer);

        if (ProjectMapActivity.bitTypeBeans.size() > 0) {
            if (bitTypeBeans == null) bitTypeBeans = new ArrayList<>();
            bitTypeBeans.clear();
            if (texttype.equals(MString.code_zero)) {
                bitTypeBeans.add(new BitTypeBean("", "全部", "全部"));
            }
            bitTypeBeans.addAll(ProjectMapActivity.bitTypeBeans);
            transformerListHandler.sendEmptyMessage(1);
        } else {
            getTransformerList();
        }
    }

    /**
     * 加载台区*
     */
    private void addViewTransFormer(BitTypeBean s) {
        MyLog.L(MString.SUCCESSED + " SystemTypeBean=" + JSON.toJSONString(s));
        RelativeLayout layout = new RelativeLayout(getActivity());
        TextView view = new TextView(getActivity());
        view.setText(" " + s.getName() + " ");
        view.setTextSize(14);
        view.setTextColor(getResources().getColor(R.color.gray));
        view.setGravity(Gravity.CENTER);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(getActivity(), 36));
        layoutParams.setMargins(0, 0, DensityUtil.dip2px(getActivity(), 8), 0);//4个参数按顺序分别是左上右下
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(view, layoutParams);
        crossing_views.add(view);
        view.setOnClickListener(new MClickListener(0, s) {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view, int position, Object msg) {
                if (crossing_views.size() > 0) {
                    for (TextView textView : crossing_views) {
                        textView.setTextColor(getResources().getColor(R.color.gray));
                        textView.setBackground(null);
                    }
                }
                view.setBackgroundResource(R.drawable.circular6_blue_line3);
                ((TextView) view).setTextColor(getResources().getColor(R.color.caikan_bule));
                explor_id = ((BitTypeBean) msg).getType();

                getSectionCorss();
            }
        });

        final float scale = getActivity().getResources().getDisplayMetrics().density;
        int left = (int) (28 * scale + 0.5f);
        crossing_transformer.setPadding(left, 0, 0, 0);

        crossing_transformer.addView(layout, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    }

/*************************************材料统计***********************************************/

    /**
     * materialstatistics*
     */
    private void findView_materialstatistics(View v) {
        RelativeLayout materialstatistics = (RelativeLayout) v.findViewById(R.id.materialstatistics_item);
        materialstatistics.setVisibility(View.VISIBLE);
        materialstatistics_transformer = (LinearLayout) v.findViewById(R.id.materialstatistics_transformer);
        if (ProjectMapActivity.bitTypeBeans.size() > 0) {
            if (bitTypeBeans == null) bitTypeBeans = new ArrayList<>();
            bitTypeBeans.clear();
            bitTypeBeans.add(new BitTypeBean("", "全部", "全部"));
            if (ProjectMapActivity.bitTypeBeans != null)
                bitTypeBeans.addAll(ProjectMapActivity.bitTypeBeans);
            transformerListHandler.sendEmptyMessage(1);
        } else {
            getTransformerList();
        }
    }

    /**
     * 加载台区*
     */
    private void addViewMaterialstatistics(BitTypeBean s) {
        RelativeLayout layout = new RelativeLayout(getActivity());
        TextView view = new TextView(getActivity());
        view.setText(" " + s.getName() + " ");
        view.setTextSize(14);
        view.setTextColor(getResources().getColor(R.color.gray));
        view.setGravity(Gravity.CENTER);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(getActivity(), 36));
        layoutParams.setMargins(0, 0, DensityUtil.dip2px(getActivity(), 8), 0);//4个参数按顺序分别是左上右下
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(view, layoutParams);
        materialstatistics.add(view);
        view.setOnClickListener(new MClickListener(0, s) {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View view, int position, Object msg) {
                if (materialstatistics.size() > 0) {
                    for (TextView textView : materialstatistics) {
                        textView.setTextColor(getResources().getColor(R.color.gray));
                        textView.setBackground(null);
                    }
                }
                view.setBackgroundResource(R.drawable.circular6_blue_line3);
                ((TextView) view).setTextColor(getResources().getColor(R.color.caikan_bule));

                explor_id = ((BitTypeBean) msg).getType();
                getMaterialSum();
            }
        });
        final float scale = getActivity().getResources().getDisplayMetrics().density;


        int left = (int) (28 * scale + 0.5f);
        materialstatistics_transformer.setPadding(left, 0, 0, 0);
        materialstatistics_transformer.addView(layout, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    }

    /**
     * **********************************用电户**********************************************
     */

    private void findView_UserPoint(View v) {
        RelativeLayout electrichousehold = (RelativeLayout) v.findViewById(R.id.electrichousehold_item);
        electrichousehold.setVisibility(View.VISIBLE);

        MyApp.File.ReadFileToSend(userPointHandler, MString.ProjectStageElectric + "_" + MyApp.currProject.getStage_id());
        getUserPoint();
    }

    @Override
    public void onDestroy() {
        details_base_Adapter = null;
        base_data = null;
        super.onDestroy();

    }
}
